<template>
  <div class="grade-list">
    <el-card>
      <div class="toolbar">
        <div class="search-area">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索学号、姓名"
            style="width: 200px"
            clearable
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
          
          <el-input
            v-model="searchSemester"
            placeholder="学期（如2023-2024-1）"
            style="width: 200px"
            clearable
          />
          
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
        </div>
        
        <div class="action-area">
          <el-button
            v-if="userStore.checkPermission(Permission.GRADE_MANAGE)"
            type="success"
            @click="showImportDialog = true"
          >
            <el-icon><Upload /></el-icon>
            批量导入
          </el-button>
        </div>
      </div>
      
      <el-table :data="gradeList" v-loading="loading" stripe style="margin-top: 20px">
        <el-table-column prop="studentId" label="学号" width="120" />
        <el-table-column prop="semester" label="学期" width="120" />
        <el-table-column prop="courseName" label="课程名称" width="150" />
        <el-table-column prop="courseCode" label="课程代码" width="120" />
        <el-table-column prop="credit" label="学分" width="80" />
        <el-table-column prop="score" label="成绩" width="80" />
        <el-table-column prop="examType" label="考试类型" width="100" />
        <el-table-column prop="teacherName" label="任课教师" width="100" />
        <el-table-column prop="createdAt" label="录入时间" width="180" />
      </el-table>
      
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSearch"
        @current-change="handleSearch"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Search, Upload } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { Grade, Permission } from '@/types'
import { getGradeList } from '@/api/grade'

const userStore = useUserStore()

const loading = ref(false)
const gradeList = ref<Grade[]>([])
const total = ref(0)
const searchKeyword = ref('')
const searchSemester = ref('')
const showImportDialog = ref(false)

const pagination = reactive({
  page: 1,
  pageSize: 20
})

const fetchGradeList = async () => {
  loading.value = true
  try {
    const res = await getGradeList({
      ...pagination,
      semester: searchSemester.value
    })
    gradeList.value = res.list
    total.value = res.total
  } catch (error) {
    console.error('获取成绩列表失败:', error)
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  pagination.page = 1
  fetchGradeList()
}

onMounted(() => {
  fetchGradeList()
})
</script>

<style scoped>
.grade-list {
  padding: 20px;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
}

.search-area {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.action-area {
  display: flex;
  gap: 10px;
}
</style>

